﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNHDAO;
using System.Data.Linq;
using QLNHBUS;

namespace PMQLNhaHang
{
    public partial class fmQLKhoSanPham : Form
    {
        SanPham sanpham;
        public fmQLKhoSanPham()
        {
            InitializeComponent();
        }

        public fmQLKhoSanPham(int masanpham)  : this()
        {
            /*this.sanpham = MonAnBUS.LayMonAn(masanpham);

            Image monanImage;
            try{
                monanImage = ByteArrayToImage(monan.Hinh.ToArray());
            }
            catch(Exception ex)
            {
                monanImage = null;
            }

            tbTenMonAn.Text = monan.Ten;
            tbGhiChu.Text = monan.GhiChu;
            cbNhomMonAn.Text = monan.NhomMonAn.Ten;
            cbDonVi.Text = monan.DonVi.Ten;
            tbGiaHienHanh.Text = monan.GiaHienHanh.ToString();
            ptbMonAn.Image = monanImage;*/
          
        }

        private void btnLuuThongTin_Click(object sender, EventArgs e)
        {
            LuuThongTin();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LuuThongTin()
        {
            try
            {
                SanPham sanpham;
                if (this.sanpham == null)
                    sanpham = new SanPham();
                else
                {
                    sanpham = this.sanpham;
                }

                int manhomsanpham = int.Parse(cbNhomSanPham.SelectedValue.ToString());
                int makho = int.Parse(cbKho.SelectedValue.ToString());

                sanpham.Ten = tbTenSanPham.Text;
                sanpham.KhoiLuong = CheckInt(tbKhoiLuong.Text);
                sanpham.NguonCungCap = tbNguonCungCap.Text;
                sanpham.HangDung = tbHangDung.Text;
                sanpham.Xoa = false;
                sanpham.GiaHienHanh = CheckInt(tbGiaHienHanh.Text);
                sanpham.NhomSanPham = NhomSanPhamBUS.LayNhomSanPham(manhomsanpham);
                sanpham.Kho = KhoBUS.LayKho(makho);
                
                if (this.sanpham == null)
                    SanPhamBUS.ThemSanPham(sanpham);
                else
                    SanPhamBUS.CapNhatSanPham(sanpham);

                MessageBox.Show("Lưu thành công.");
            }
            catch (Exception)
            {
                MessageBox.Show("Lưu thất bại");
            }
        }

        private int CheckInt(string str)
        {
            int result;
            try
            {
                result = int.Parse(str);
            }
            catch (Exception)
            {
                result = 0;
            }

            return 0;
        }

        private void fmQLKhoSanPham_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            var lstNhomSanPham = NhomSanPhamBUS.LayDanhSachNhomSanPhamChuaXoa();
            var lstKho = KhoBUS.LayDanhSachKhoChuaXoa();

            cbNhomSanPham.DisplayMember = "Ten";
            cbNhomSanPham.ValueMember = "MaNhomSanPham";
            cbNhomSanPham.DataSource = lstNhomSanPham;

            cbKho.DisplayMember = "Ten";
            cbKho.ValueMember = "MaKho";
            cbKho.DataSource = lstKho;
        }
    }
}
